package cn.crc.pjblog.componentsdata;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;


import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tapestry5.util.AbstractSelectModel;
/**
 * 修改依赖(commons-beanutils)的包为T内建的PropertyAccess服务
 * 
 * */
public class GenericSelectionModel<T> extends AbstractSelectModel {
	
	 private String labelField;

     private List<T> list;
     
     private final PropertyAccess adapter;
     
     public GenericSelectionModel(List<T> list, String labelField,PropertyAccess adapter) {
             this.labelField = labelField;
             this.list = list;
             this.adapter = adapter;
             
     }

     public List<OptionGroupModel> getOptionGroups() {
             return null;
     }

     public List<OptionModel> getOptions() {
             List<OptionModel> optionModelList = new ArrayList<OptionModel>();
             try {
                     for (T obj : list) {
                             if (labelField == null) {
                                     optionModelList.add(new OptionModelImpl(obj + "",  new String[0]));
                             } else {
                                     optionModelList.add(new OptionModelImpl(
                                                     adapter.get(obj, labelField)+"",  new String[0]));
                             }
                     }
             } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();             
             }
             return optionModelList;
     }


}
